package com.hjx.windowtool.imgproc;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JSplitPane;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.text.JTextComponent;

import com.hjx.windowtool.db.Tool;
import com.hjx.windowtool.util.ImgUtil;
import com.hjx.windowtool.util.StrUtil;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

@Tool("图片批量处理")
public class ImageProcessApp {

	private JFrame frame;
	private JTextField txt_srcdir;
	private JTextField txt_targetdir;
	private JTextField txt_mskmin;
	private JTextField txt_mskmax;
	private JTextField txt_mskSize;
	private JTextField txt_fomatName;
	private JScrollPane scrollPane;
	private JTextArea txt_msg;
	private JTextField txt_resizewidth;
	private JTextField txt_resizeheight;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					ImageProcessApp window = new ImageProcessApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ImageProcessApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 643, 541);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.3);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		frame.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(null);
		
		JLabel label = new JLabel("输入图片");
		label.setBounds(10, 10, 54, 15);
		panel.add(label);
		
		JLabel label_1 = new JLabel("输出图片");
		label_1.setBounds(10, 35, 54, 15);
		panel.add(label_1);
		
		txt_srcdir = new JTextField();
		setDoubleClickSelectFille(txt_srcdir);
		txt_srcdir.setBounds(61, 7, 374, 21);
		panel.add(txt_srcdir);
		txt_srcdir.setColumns(10);
		txt_srcdir.putClientProperty("type", "srcdir");
		
		txt_targetdir = new JTextField();
		txt_targetdir.setBounds(61, 32, 374, 21);
		panel.add(txt_targetdir);
		txt_targetdir.setColumns(10);
		setDoubleClickSelectFille(txt_targetdir);
		txt_targetdir.putClientProperty("type", "targetdir");
		
		JButton btnmsk = new JButton("随机MSK");
		btnmsk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				randomMasaike(txt_srcdir.getText(),txt_targetdir.getText(),Integer.valueOf(txt_mskmin.getText()),
						Integer.valueOf(txt_mskmax.getText()),Integer.valueOf(txt_mskSize.getText()),txt_fomatName.getText());
			}
		});
		btnmsk.setBounds(226, 66, 93, 23);
		panel.add(btnmsk);
		
		txt_mskmin = new JTextField();
		txt_mskmin.setText("40");
		txt_mskmin.setBounds(61, 67, 34, 21);
		panel.add(txt_mskmin);
		txt_mskmin.setColumns(10);
		txt_mskmin.putClientProperty("type", "mskmin");
		
		txt_mskmax = new JTextField();
		txt_mskmax.setText("100");
		txt_mskmax.setBounds(102, 67, 34, 21);
		panel.add(txt_mskmax);
		txt_mskmax.setColumns(10);
		txt_mskmax.putClientProperty("type", "mskmax");
		
		txt_mskSize = new JTextField();
		txt_mskSize.setText("8");
		txt_mskSize.setBounds(140, 67, 26, 21);
		panel.add(txt_mskSize);
		txt_mskSize.setColumns(10);
		txt_mskSize.putClientProperty("type", "mskSize");
		
		txt_fomatName = new JTextField();
		txt_fomatName.setText("png");
		txt_fomatName.setBounds(176, 67, 40, 21);
		panel.add(txt_fomatName);
		txt_fomatName.setColumns(10);
		
		txt_resizewidth = new JTextField();
		txt_resizewidth.setText("1920");
		txt_resizewidth.setBounds(345, 67, 34, 21);
		panel.add(txt_resizewidth);
		txt_resizewidth.setColumns(10);
		
		JLabel lblW = new JLabel("W");
		lblW.setBounds(323, 70, 12, 15);
		panel.add(lblW);
		
		JLabel lblH = new JLabel("H");
		lblH.setBounds(389, 70, 12, 15);
		panel.add(lblH);
		
		txt_resizeheight = new JTextField();
		txt_resizeheight.setText("1080");
		txt_resizeheight.setBounds(407, 67, 34, 21);
		panel.add(txt_resizeheight);
		txt_resizeheight.setColumns(10);
		
		JButton button = new JButton("改大小");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resizeImages(txt_srcdir.getText(),txt_targetdir.getText(),Integer.valueOf(txt_resizewidth.getText()),Integer.valueOf(txt_resizeheight.getText()));
			}
		});
		button.setBounds(451, 66, 70, 23);
		panel.add(button);
		txt_fomatName.putClientProperty("type", "fomatName");
		
		JPanel panel_1 = new JPanel();
		splitPane.setRightComponent(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		scrollPane = new JScrollPane();
		panel_1.add(scrollPane);
		
		txt_msg = new JTextArea();
		scrollPane.setViewportView(txt_msg);
		loadText(frame.getContentPane());
	}

	protected void resizeImages(String src, String target, int width, int height) {
		List<File> list = getImageFiles(src);
		for (File file : list) {
			BufferedImage image = ImgUtil.resizeImage(file,width,height);
			ImgUtil.saveImage(image, new File(target,file.getName()), file.getName().substring(file.getName().lastIndexOf(".")+1));
		}
	}

	/**
	 * 
	 */
	public void setDoubleClickSelectFille(JTextField txt) {
		txt.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount()==2) {
					File file = StrUtil.selectToOpen(txt);
					if (file==null) {
						return;
					}
					txt.setText(file.getAbsolutePath());
				}
			}
		});
	}

	protected void randomMasaike(String src, String tar, int min, int max,int size, String formatName) {
		List<File> list = getImageFiles(src);
		for (File file : list) {
			BufferedImage image=ImgUtil.randomMarkImage(file,min*1d/100d,max*1d/100d,size);
			try {
				ImageIO.write(image, formatName, new File(tar,file.getName().substring(0, file.getName().lastIndexOf("."))+"."+formatName));
				setMsg(file+":处理成功");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		saveText(frame.getContentPane());
	}

	/**
	 * @param src
	 * @return
	 */
	public List<File> getImageFiles(String src) {
		List<File> list=new ArrayList<>();
		File fsrc = new File(src);
		if (fsrc.isDirectory()) {
			list=Arrays.asList(fsrc.listFiles());
		}else{
			list.add(fsrc);
		}
		List<String> listFileTypes = Arrays.asList("png","jpeg","jpg","bmp");
		list=list.stream().filter(new Predicate<File>() {
			@Override
			public boolean test(File t) {
				String subfix = t.getName().substring(t.getName().lastIndexOf(".")+1);
				return listFileTypes.contains(subfix);
			}
		}).collect(Collectors.toList());
		return list;
	}

	private void setMsg(String string) {
		txt_msg.append(string+"\n");
		txt_msg.setCaretPosition(txt_msg.getDocument().getLength());
	}

	private void saveText(Container com) {
		Component[] components = com.getComponents();
		for (Component component : components) {
			if (component instanceof JTextComponent) {
				Object object = ((JTextComponent) component).getClientProperty("type");
				if (object!=null) {
					StrUtil.saveObject(object+"", ((JTextComponent) component).getText());
				}
			}else{
				if (component instanceof Container) {
					saveText((Container) component);
				}
			}
		}
	}
	private void loadText(Container com){
		Component[] components = com.getComponents();
		for (Component component : components) {
			if (component instanceof JTextComponent) {
				Object object = ((JTextComponent) component).getClientProperty("type");
				if (object!=null) {
					Object readObject = StrUtil.readObject(object+"");
					((JTextComponent) component).setText(readObject==null?"":readObject.toString());
				}
			}else{
				if (component instanceof Container) {
					loadText((Container) component);
				}
			}
		}
	}
}
